# coding=utf-8

from functools import wraps
from flask import request
from flask_jwt_extended import JWTManager, verify_jwt_in_request, get_current_user,\
    create_access_token, create_refresh_token,get_jwt

identity = dict(uid=0, scope='newness')
def get_tokens(user, verify_remote_addr=False):
    identity['uid'] = user.uid
    if verify_remote_addr:
        identity['remote_addr'] = request.remote_addr
    access_token = create_access_token(identity=identity)
    refresh_token = create_refresh_token(identity=identity)
    return access_token, refresh_token


def admin_required(fn):
    @wraps(fn)
    def wrapper(*args, **kwargs):
        verify_jwt_in_request()
        #不返回任何东西。如果令牌解码链中的任何事情失败，它将返回一个适当的异常。也就是说它在这里的作用是为了校验当前请求有没有token, 如果没有则会返回一个异常信息，不能访问被保护的节点，同样的和 @ jwt_required()
        #它们一样有四个可选的参数：optional = False, fresh = False, refresh = False, locations = None，这里暂时不说，后面统一描述。
       # claims = get_jwt()

        #current_user = get_current_user()
        #get_jwt_claims = get_jwt_claims()
        #return 'false'
        return fn(*args, **kwargs)

    return wrapper